//
//  HZSingleStockViewController.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZSingleStockViewController.h"
#import "HZSingleStockView.h"
#import "HZSearchViewController.h"
#import "HZNavHeaderView.h"
#import "HZDataTool.h"
#import "HZSearchTool.h"

@interface HZSingleStockViewController ()
{
    int _nMinPriceId;
    int _nMinDetailId;
    int _nMinId;
    int _nIndexZhangFuId;
    int _nIndexDieFuId;
    int _nIndexHuanShouId;
    int _nKLineId;
}
@property (nonatomic, strong)HZNavHeaderView *pNavHeaderView;
@property (nonatomic, strong)HZSingleStockView *pSingleView;
@property (nonatomic, assign)BOOL isHiddenStatusBar;
@property (nonatomic, strong)HZOwnedStockModel *pCodeInfo;
@end

@implementation HZSingleStockViewController

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithCodeInfo:(HZOwnedStockModel *)codeInfo{
    self = [super init];
    if (self) {
        _pCodeInfo = codeInfo;
        self.logicManager = [HZBaseLogjcManager new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    [self setRightBarBtnItemImage:@"head-search" title:nil];
    _pNavHeaderView = [[HZNavHeaderView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
    self.navigationItem.titleView = _pNavHeaderView;
    
    _pSingleView = [HZSingleStockView new];
    _pSingleView.parent = self;
    _pSingleView.logicManager = self.logicManager;
    
    [self.view addSubview:_pSingleView];
    
    [self p_initConstraints];
    
    _isHiddenStatusBar = NO;
    
    _pCodeInfo.nModel_isR = [HZDataTool getIsRStockWithCodeInfo:_pCodeInfo];
    
    //注册代码块
    [self p_registerBlock];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_pSingleView bindCodeInfo:_pCodeInfo];
    [_pNavHeaderView updateCodeInfo:_pCodeInfo];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    @try {
        [self p_subscribeSingleStock];
        [self p_subscribeKLineWithKType:33];//默认日K
    //    [_pSingleView bindCodeInfo:_pCodeInfo];
    //    [_pNavHeaderView updateCodeInfo:_pCodeInfo];
        
        if ([HZUserManager manager].isVip) {
            [HZUserManager manager].peiziWindow.hidden = NO;
            [HZUserManager manager].peiziWindow.baseVC = self;
        }
    } @catch (NSException *exception) {
        
    } @finally {
        
    }
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [HZUserManager manager].peiziWindow.hidden = YES;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [HZNetworkTool networkCacelSubscribeId:_nMinPriceId];
    [HZNetworkTool networkCacelSubscribeId:_nMinDetailId];
    [HZNetworkTool networkCacelSubscribeId:_nMinId];
    [HZNetworkTool networkCacelSubscribeId:_nIndexZhangFuId];
    [HZNetworkTool networkCacelSubscribeId:_nIndexDieFuId];
    [HZNetworkTool networkCacelSubscribeId:_nIndexHuanShouId];
    [HZNetworkTool networkCacelSubscribeId:_nKLineId];
}

- (BOOL)shouldAutorotate{
    return NO;
}

- (BOOL)prefersStatusBarHidden{
    return _isHiddenStatusBar;
}
- (void)dealloc {
    DLog(@"singleStock销毁");
}
#pragma mark -
#pragma mark private
- (void)p_initConstraints
{
    [_pSingleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}

- (void)rightBarBtnOnClick{
    [self.parent upToParentWithObjc:self withType:kHZ_presentSearchVC_Action withParamter:nil];
}

- (void)p_subscribeSingleStock
{
    //避免循环引用
    __weak typeof (self)weakSelf = self;
    _nMinPriceId = [HZNetworkTool networkSubscribeDataWithapiType:kHZApi_MinPrice_Type code:_pCodeInfo.model_Code market:_pCodeInfo.nModel_Market success:^(id model, kHZApiType apiType) {
        DLog(@"======%@",model);
        [weakSelf.pSingleView downToChildWithObjc:[NSNumber numberWithInt:apiType] withType:kHZ_updateNewData_Action withParamter:model];
    }];
    _nMinDetailId = [HZNetworkTool networkSubscribeDataWithapiType:kHZApi_MinDetail_Type code:_pCodeInfo.model_Code market:_pCodeInfo.nModel_Market success:^(id model, kHZApiType apiType) {
        [weakSelf.pSingleView downToChildWithObjc:[NSNumber numberWithInt:apiType] withType:kHZ_updateNewData_Action withParamter:model];
    }];
    _nMinId = [HZNetworkTool networkSubscribeDataWithapiType:kHZApi_Min_Type code:_pCodeInfo.model_Code market:_pCodeInfo.nModel_Market success:^(id model, kHZApiType apiType) {
        [weakSelf.pSingleView downToChildWithObjc:[NSNumber numberWithInt:apiType] withType:kHZ_updateNewData_Action withParamter:model];
    }];
    
    if (_pCodeInfo.nModel_Style == 1) {//okex
        _nIndexZhangFuId = [HZNetworkTool networkSubscribeDataWithapiType:kHZApi_IndexZhangFuTop_Type code:_pCodeInfo.model_Code market:_pCodeInfo.nModel_Market success:^(id model, kHZApiType apiType) {
            [weakSelf.pSingleView downToChildWithObjc:[NSNumber numberWithInt:apiType] withType:kHZ_updateNewData_Action withParamter:model];
        }];
        _nIndexHuanShouId = [HZNetworkTool networkSubscribeDataWithapiType:kHZApi_IndexHuanShouTop_Type code:_pCodeInfo.model_Code market:_pCodeInfo.nModel_Market success:^(id model, kHZApiType apiType) {
            [weakSelf.pSingleView downToChildWithObjc:[NSNumber numberWithInt:apiType] withType:kHZ_updateNewData_Action withParamter:model];
        }];
        _nIndexDieFuId = [HZNetworkTool networkSubscribeDataWithapiType:kHZApi_IndexDieFuTop_Type code:_pCodeInfo.model_Code market:_pCodeInfo.nModel_Market success:^(id model, kHZApiType apiType) {
            [weakSelf.pSingleView downToChildWithObjc:[NSNumber numberWithInt:apiType] withType:kHZ_updateNewData_Action withParamter:model];
        }];

    }
}

- (void)p_subscribeKLineWithKType:(int)kType
{
    //避免循环引用
    [HZNetworkTool networkCacelSubscribeId:_nKLineId];
    __weak typeof (self)weakSelf = self;
    _nKLineId = [HZNetworkTool networkSubscribeDataWithapiType:kHZApi_KLine_Type code:_pCodeInfo.model_Code market:_pCodeInfo.nModel_Market kType:kType success:^(id model, kHZApiType apiType) {
        [weakSelf.pSingleView downToChildWithObjc:[NSNumber numberWithInt:apiType] withType:kHZ_updateNewData_Action withParamter:model];
    }];
}
// 上传自选股
- (void)p_uploadRequest
{
    NSArray *arr = [HZNetworkTool specialGetStocks];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"300031", kAPIFuctionKey,
                          arr, @"param",
                          nil];
    [HZApiManager apiPostParams:dict success:^(id objc) {
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}
#pragma mark -
#pragma mark blockAction
- (void)p_registerBlock
{
    //避免循环引用
    __weak typeof (self)weakSelf = self;
    [self.logicManager mapLogic:kLogicRouterAddOwn toBlock:^id(NSDictionary *params, id other) {
        if (![weakSelf checkIsLogined]) { return nil;}
        int nResult = -1;
        if ([other isEqualToString:@"加自选"]) {
            nResult = [HZNetworkTool networkAddOwnStock:weakSelf.pCodeInfo.model_Code WithMarketId:weakSelf.pCodeInfo.nModel_Market];
            [HZNetworkTool speicalAddOwnStock:@{@"code": weakSelf.pCodeInfo.model_Code, @"market": [NSString stringWithFormat:@"%d", weakSelf.pCodeInfo.nModel_Market], @"name": weakSelf.pCodeInfo.model_Name}];
        }else if ([other isEqualToString:@"删自选"]) {
            nResult = [HZNetworkTool networkDeleteOwnStock:weakSelf.pCodeInfo.model_Code WithMarketId:weakSelf.pCodeInfo.nModel_Market];
        }
        [weakSelf p_uploadRequest];
        return [NSNumber numberWithInt:nResult];
    }];
    [self.logicManager mapLogic:kLogicRouterCreateStategy toBlock:^id(NSDictionary *params, id other) {
        if (![weakSelf checkIsLogined]) { return nil;}
        UIViewController *nextVC = [[MYRouter shared] matchController:kRouterCreatStategy params:@{
                                                                                                   @"code":weakSelf.pCodeInfo.model_Code,
                                                                                                   @"market":@(weakSelf.pCodeInfo.nModel_Market),
                                                                                                   @"try":@(_isTry),
                                                                                                   @"bs": other,
                                                                                                   }];
        [weakSelf.navigationController pushViewController:nextVC animated:YES];
        return nil;
    }];
}
- (BOOL)checkIsLogined
{
    BOOL isLogined = [HZUserManager manager].isLogined;
    if (!isLogined) {
        UIViewController *nextVC = [[MYRouter shared] matchController:kRouterLogin];
        if(nextVC)
        [self.navigationController presentViewController:nextVC animated:YES completion:nil];
    }
    return isLogined;
}

#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_setLandscapeScreen_Action:
        {
            [HZUserManager manager].peiziWindow.hidden = YES;
            _isHiddenStatusBar = YES;
            [MYCommonTool toolChangeScreenOrientation:UIInterfaceOrientationLandscapeRight withViewController:self];
//            [_pSingleView updateNewConstraints:kHZSingleStock_landscape_State];
        }
            break;
        case kHZ_cancelLandscapeScreen_Action:
        {
            [HZUserManager manager].peiziWindow.hidden = NO;
            _isHiddenStatusBar = NO;
            [MYCommonTool toolChangeScreenOrientation:UIInterfaceOrientationPortrait withViewController:self];
//            [_pSingleView updateNewConstraints:kHZSingleStock_portrait_State];
        }
            break;
        case kHZ_jumpSingleStock_Action:
        {
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZ_ClickKLineRequest_Action:
        {
            int nKType = [paramter intValue];
            [self p_subscribeKLineWithKType:nKType];
        }
            break;
            
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}

@end
